﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class DeliverBoxViewModel:NodeViewModel
    {
         public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bottle.png", UriKind.RelativeOrAbsolute);
        }
        
        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");

         public DeliverBoxViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "DeliverBoxViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            GetList();
        }

         private ObservableCollection<BottleInBox> bottleInBox;
         public ObservableCollection<BottleInBox> BottleInBox
         {
             get { return bottleInBox; }
             set
             {
                 bottleInBox = value;
                 NotifyChanged("BottleInBox");
             }
         }

         private string _errorMSG = null;
         public string ErrorMSG
         {
             get { return _errorMSG; }
             set
             {
                 _errorMSG = value;
                 NotifyChanged("ErrorMSG");
             }
         }
         public DeliverBoxViewModel()
         {
             GetList();

         }

         public void GetList()
         {
             var list = sp.BottleInBox();
             bottleInBox = new ObservableCollection<BottleInBox>();
             foreach (var item in list)
             {
                 var bb = new BottleInBox();
                 bb.ID = item.ID;
                 bb.AnalysBatchID = item.AnalysGroup.质检凭证号;
                 bb.Materiel = item.AnalysGroup.物料.Name;
                 bb.BottleID = item.BottleID;
                 bb.BottleSaveTime = item.BottleSaveTime;

                 bottleInBox.Add(bb);
             }
             this.BottleInBox = bottleInBox;
         }

         public ICommand Query
         {
             get
             {
                 return new DelegateCommand(() =>
                 {
                     GetList();
                 });
             }
         }


         public ICommand Quit
         {
             get
             {
                 return new DelegateCommand(() =>
                 {
                     int i = 0;
                     this.ErrorMSG = null;
                     bottleInBox = this.BottleInBox;
                     foreach (var item in bottleInBox)
                     {

                         //调用发送封桶指令的接口

                         if (sp.QuitBox(item.ID)) i++;

                     }
                     //GetList();
                     if (i > 0) this.ErrorMSG = "本次样桶箱中装有" + i.ToString() + "个试样瓶！取走样桶箱后请刷新列表。";
                 });
             }
         }

    }

    public class BottleInBox
    {
        public BottleInBox() { }

        public Guid ID
        {
            get;
            set;
        }

        public string AnalysBatchID { get; set; }
        public string Materiel { get; set; }
        public string BottleID { get; set; }
        public DateTime? BottleSaveTime { get; set; }
    }
}
